import { historyPositionApi, keYongMoneyApi, zhiMaYingKuiApi, ziChanApi } from '@/api';
import { isLocal } from '@/utils';
import { useRequest } from 'ahooks';
import { Button, Space } from 'antd';
import dayjs from 'dayjs';
import { useEffect, useState } from 'react';
export default function Test() {
  const [liShiList, setLiShiList] = useState([]);
  const [loadingNew, setLoadingNew] = useState(false);

  useEffect(() => {
    setTimeout(() => {
      // getLiShiCangWei();
    }, 100);
  }, []);

  // 芝麻盈亏(旧)
  const { loading, run: getZiChan } = useRequest(async () => ziChanApi(), {
    // manual: true,
    onSuccess: (res: any) => {
      console.log('res:芝麻盈亏(旧) ', res.data);
    },
  });

  // 获取可用资产
  const { loading: loadingKY, run: getKYZC } = useRequest(
    async (p) => {
      if (p?.isNew) setLoadingNew(true);
      const res: any = (await keYongMoneyApi(p)).data;
      console.log(`res:获取可用资产-${p?.isNew ? '新' : '旧'}`, res);
      if (p?.isNew) setLoadingNew(false);
      return res;
    },
    {
      // manual: true,
    },
  );

  // 获取历史仓位
  async function getLiShiCangWei() {
    const params = {
      from: 1740413327, // 24
      to: 1741079294,
    };
    // from: 1733155200, to: 1740931199, 18-3.2
    // 1740413327 1741079294
    const res: any = (await historyPositionApi(params))?.data?.map((item: any, i: number) => ({
      ...item,
      id: i + 1,
    }));
    console.log('res:获取历史仓位 ', res);
    setLiShiList(res);
  }

  // 芝麻盈亏
  const { loading: YKLading, run: getZhiMaYingKui } = useRequest(async () => zhiMaYingKuiApi(), {
    // manual: true,
    onSuccess: (res: any) => {
      console.log('res:芝麻盈亏(新) ', res?.data);
    },
  });

  return (
    <div className="testpage">
      {isLocal() && (
        <>
          <Space>
            <Button loading={loading} onClick={() => getZiChan()}>
              芝麻盈亏(旧)
            </Button>
            <Button loading={loadingKY} onClick={() => getKYZC({})}>
              获取可用资产(旧)
            </Button>
            <Button loading={YKLading} onClick={() => getZhiMaYingKui()}>
              芝麻盈亏(新)
            </Button>
            <Button loading={loadingNew} onClick={() => getKYZC({ isNew: true })}>
              获取可用资产(新)
            </Button>
          </Space>
          <div>
            <div>历史仓位，total{liShiList?.length}</div>
            {liShiList.map((item: any, i) => (
              <div key={i}>
                {item?.id}-{dayjs(item?.time * 1000).format('YYYY-MM-DD HH:mm:ss')}
              </div>
            ))}
          </div>
        </>
      )}
    </div>
  );
}
